#include "alarm.h"

#include "user.h"
#include "db.h"
#include "global.h"

#include <qjsonobject.h>

Alarm::Alarm(QObject *parent)
    : QObject{parent}
{}

bool Alarm::getAlarmParams()
{

    DB* db = DB::getInstance();
    return db->getAlarmParams(m_speed, m_temperature, m_weight, m_ph, m_conductivity);
}

bool Alarm::saveAlarmParam(const QString &param, const QString &value, const QString& name)
{
    DB* db = DB::getInstance();
    db->saveParamValue(param,value,name);

    QJsonObject jsonObj;
    jsonObj["param"]   = param;
    jsonObj["value"] = value;                  // 整数
    jsonObj["name"] = name;              // 字符串

    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    QString realName = pUser->currentLogin()["real_name"].toString();

    QJsonDocument jd(jsonObj);
    // DB::getInstance()->addLog("modifyParam", name, param, jd.toJson(QJsonDocument::Indented), name);
    DB::getInstance()->addLog(loginName, realName, "保存参数", "保存成功", param+"->"+value,loginName);
    return true;
}

bool Alarm::saveAlarmAll(QHash<QString, QVariant> speed, QHash<QString, QVariant> weight, QHash<QString, QVariant> temperature, QHash<QString, QVariant> ph, QHash<QString, QVariant> conductivity)
{
    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    DB::getInstance()->saveAlarmAll(speed, weight, temperature, ph, conductivity, loginName);
    m_speed=speed;
    m_weight=weight;
    m_temperature=temperature;
    m_ph=ph;
    m_conductivity=conductivity;
    return true;
}

bool Alarm::saveAlarm(QHash<QString, QVariant> param, const QString &type)
{
    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    DB::getInstance()->saveAlarmParam(param, loginName, type);
    if (type.compare("speed") ==0){
        m_speed = param;
    }
    if (type.compare("weight") ==0){
        m_weight = param;
    }
    if (type.compare("ph") ==0){
        m_ph = param;
    }
    if (type.compare("temperature") ==0){
        m_temperature = param;
    }
    if (type.compare("conductivity") ==0){
        m_conductivity = param;
    }
    return true;
}

QHash<QString, QVariant> Alarm::getSpeedParam()
{
    qDebug() << "ll" <<m_speed.size()<< m_speed["ll"];
    return m_speed;
}

QHash<QString, QVariant> Alarm::getWeightParam()
{
    QHash<QString, QVariant>::const_iterator it;
    for (it = m_temperature.constBegin(); it != m_temperature.constEnd(); ++it) {
        qDebug() << "Key:" << it.key() << "Value:" << it.value();
    }
    return m_weight;
}

QHash<QString, QVariant> Alarm::getTemperatureParam()
{
    // QHash<QString, QVariant>::const_iterator it;
    // for (it = m_temperature.constBegin(); it != m_temperature.constEnd(); ++it) {
    //     qDebug() << "Key:" << it.key() << "Value:" << it.value();
    // }

    return m_temperature;
}

QHash<QString, QVariant> Alarm::getPhParam()
{
    return m_ph;
}

QHash<QString, QVariant> Alarm::getConductivityParam()
{
    return m_conductivity;
}

QString Alarm::getAlarmParam(const QString &param)
{
    DB* db = DB::getInstance();
    return db->getAlarmParam(param);
}

int Alarm::checkAlarm(const QString &type, const QString desc, float value)
{
    QHash<QString, QVariant>& hs = get(type);
    return checkAlarm(hs, type, desc, value);
}

int Alarm::checkAlarm(QHash<QString, QVariant>& temp, const QString &type, const QString desc, float value)
{
    if(temp["llStatus"] == 1){
        if (value < temp["ll"].toFloat()){
            DB::getInstance()->insertAlaramLog(type, QString("%1 低于低低限").arg(desc), QString("%1").arg(value), temp["ll"].toString(), temp["llStatus"].toInt());
            return 0;
        }
    }
    if(temp["lStatus"] == 1){
        if (value < temp["l"].toFloat()){
            DB::getInstance()->insertAlaramLog(type, QString("%1 低于低限").arg(desc), QString("%1").arg(value), temp["l"].toString(), temp["lStatus"].toInt());
            return 1;
        }
    }
    if(temp["hhStatus"] == 1){
        if (value > temp["hh"].toFloat()){
            DB::getInstance()->insertAlaramLog(type, QString("%1 高于高高限").arg(desc), QString("%1").arg(value), temp["hh"].toString(), temp["hhStatus"].toInt());
            return 0;
        }
    }
    if(temp["hStatus"] == 1){
        if (value > temp["h"].toFloat()){
            DB::getInstance()->insertAlaramLog(type, QString("%1 高于高限").arg(desc), QString("%1").arg(value), temp["h"].toString(), temp["hStatus"].toInt());
            return 1;
        }
    }
    return 2;

}

QHash<QString, QVariant> &Alarm::get(const QString &type)
{
    if (type.compare("speed") == 0) return m_speed;
    if (type.compare("weight") == 0) return m_weight;
    if (type.compare("temperature") == 0) return m_temperature;
    if (type.compare("ph") == 0) return m_ph;

    return m_conductivity;
}

